package com.zhongyouyun.lbyc.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongyouyun.admin.model.UserModel;
import com.zhongyouyun.lbyc.model.CustomModernEvaluateModel;
import com.zhongyouyun.lbyc.model.CustomModernSalesVolumeModel;
import com.zhongyouyun.lbyc.pojo.CustomModernEvaluatePojo;
import com.zhongyouyun.lbyc.pojo.CustomModernSalesVolumePojo;
import com.zhongyouyun.other.annotation.DataAuthScope;
import com.zhongyouyun.other.model.PageRequest;
import com.zhongyouyun.other.model.Result;
import com.zhongyouyun.utils.ModelUtils;
import com.zhongyouyun.utils.QueryUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zhongyouyun.lbyc.service.CustomModernSalesVolumeService;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * @Author: shenyi
 * @Date: 2023-11-14
 * @Description: 
 */
@RestController
@RequestMapping("/lbyc/lbyc_custom_modern_sales_volume")
@Api(value = "烟草灵宝-多指标营销部销售汇总controller", tags = {"烟草灵宝-多指标营销部销售汇总接口"})
public class CustomModernSalesVolumeController {
    @Autowired
    private CustomModernSalesVolumeService service;


    @PostMapping("/findPage")
    @ResponseBody
    @ApiOperation(value = "多指标营销部销售汇总分页列表", notes = "多指标营销部销售汇总分页列表")
    @DataAuthScope(deptAlias = "temp_data_scope.")
    public IPage<CustomModernSalesVolumePojo> findPage(@ApiParam(required = true) @RequestBody PageRequest pageRequest) {
        QueryWrapper<CustomModernEvaluatePojo> queryWrapper = new QueryWrapper();
        QueryUtils.fiterQuery(queryWrapper, pageRequest);
        Page page = new Page(pageRequest.getPageNum(), pageRequest.getPageSize());
        return service.selectPojoPage(page, queryWrapper);
    }


    @PostMapping("/saveOrUpate")
    @ResponseBody
    @ApiOperation(value = "修改或者新增", notes = "通过ID判断修改或者新增")
    public Result saveOrUpate(@RequestBody CustomModernSalesVolumeModel customModernSalesVolumeModel) {
        UserModel userModel = (UserModel) SecurityUtils.getSubject().getPrincipal(); // 获取当前登录用户
        if (ObjectUtil.isNotEmpty(customModernSalesVolumeModel.getId()) && ObjectUtil.isNotEmpty(customModernSalesVolumeModel.getId())) {
            userModel.setUpdateBy(userModel.getId().toString());
        } else {
            ModelUtils modelutils = new ModelUtils();
            modelutils.initModel(customModernSalesVolumeModel, userModel.getId().toString());
            customModernSalesVolumeModel.setDeptId(userModel.getDeptId());
        }
        Boolean flag = service.saveOrUpdate(customModernSalesVolumeModel);
        if (flag) {
            return Result.builder().message("成功").status(0).build();
        } else {
            return Result.builder().message("保存失败").success(false).status(1).build();
        }
    }


    @GetMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "删除", notes = "通过ID删除")
    public Result delete(@ApiParam(name = "按钮ID", value = "id", required = true) @RequestParam(value = "id") String id) {
        Boolean flag = false;
        flag = service.removeById(Long.valueOf(id));
        if (flag) {
            return Result.builder().message("成功").status(0).build();
        } else {
            return Result.builder().message("删除失败").success(false).status(1).build();
        }
    }

    @PostMapping("/batchDelete")
    @ResponseBody
    @ApiOperation(value = "批量删除", notes = "通过ID删除")
    public Result batchDelete(@RequestBody List<String> ids) {
        List<Long> list = new ArrayList<Long>();
        ids.forEach(id -> {
            list.add(Long.valueOf(id));
        });
        Boolean flag = service.removeByIds(list);
        if (flag) {
            return Result.builder().message("成功").status(0).build();
        } else {
            return Result.builder().message("删除失败").success(false).status(1).build();
        }
    }
    @PostMapping("/export/{quarter}")
    @ResponseBody
    @ApiOperation(value = "多指标营销部销售数据导入", notes = "多指标营销部销售数据导入")
    public Result export(MultipartFile file,@ApiParam(name = "quarter", value = "quarter", required = true) @PathVariable String quarter) throws IOException {

        Boolean flag = service.export(file,quarter);
        if (flag) {
            return Result.builder().message("成功").status(0).build();
        }
        return Result.builder().message("成功").status(1).build();
    }
}
